//
//  HMHomeTableViewController.swift
//  Faner_Weibo
//
//  Created by 范英强 on 16/1/21.
//  Copyright © 2016年 范英强. All rights reserved.
//

import UIKit

private let HMStatusTableViewCellIdentifier = "CELL"

class HMHomeTableViewController: HMVisitorViewController {
    //
    lazy var statusListViewModel: HMStatusListViewModel = HMStatusListViewModel()

    override func viewDidLoad() {
        super.viewDidLoad()
        
    }
    /**
     设置界面
     */
    func setupUI(){
        setupTableView()
    }
    
    override func setLoginBarButtonItem() {
        super.setLoginBarButtonItem()
        navigationItem.leftBarButtonItem = UIBarButtonItem(imgName: "navigationbar_friendsearch", target: self, action: "friendsearch")
        navigationItem.rightBarButtonItem = UIBarButtonItem(imgName: "navigationbar_pop", target: self, action: "friendsearch")
        setupUI()
        loadData()

    }
    
    private func setupTableView(){
        tableView.registerClass(HMStatusCell.self, forCellReuseIdentifier: HMStatusTableViewCellIdentifier)
//        tableView.rowHeight = 300
        tableView.rowHeight = UITableViewAutomaticDimension
        tableView.estimatedRowHeight = 200
        tableView.separatorStyle = .None
        tableView.tableFooterView = actView
        
        let refreshView: HMRefreshControl = HMRefreshControl()
        refreshView.addTarget(self, action: "test:", forControlEvents: UIControlEvents.ValueChanged)
        tableView.addSubview(refreshView)
    }
    
    func test(refresh: UIRefreshControl){
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (Int64)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), { () -> Void in
            print("结束刷新")
            refresh.endRefreshing()
        })
    }
    
    // MARK: - 点击事件
    @objc private func friendsearch() {
        print(__FUNCTION__)// 那个方法 
        print(__LINE__)//哪一行
        print(__FILE__)//那个文件
        let vc = HMTempViewController()
        navigationController?.pushViewController(vc, animated: true)
    }
    
    private lazy var actView: UIActivityIndicatorView = {
        let view = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.WhiteLarge)
        view.color = UIColor.redColor()
        return view
    }()

}

extension HMHomeTableViewController{
    
    private func loadData(){
        statusListViewModel.loadData(actView.isAnimating()) { (isSuccess) -> () in
            self.actView.stopAnimating()
            if isSuccess {
                self.tableView.reloadData()
            }else{
                
            }
        }

    }
    
}

// tableView delegate  dataSource
extension HMHomeTableViewController {
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return statusListViewModel.statusList?.count ?? 0
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier(HMStatusTableViewCellIdentifier, forIndexPath: indexPath) as! HMStatusCell
        
        cell.statusViewModel = statusListViewModel.statusList![indexPath.row]
        
//        var cell = tableView.dequeueReusableCellWithIdentifier(HMStatusTableViewCellIdentifier)
//        
//        if cell == nil{
//            cell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: HMStatusTableViewCellIdentifier)
//        }

        return cell
    }
    
    override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
        if !actView.isAnimating() && indexPath.row == statusListViewModel.statusList!.count - 1 {
            actView.startAnimating()
            loadData()
        }
    }
}
